package com.help10000.rms.ui.fragments

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import com.amir.common.api.download.LoginPageEvent
import com.amir.common.utils.ButtonUtils
import com.amir.common.utils.ToastUtil
import com.amir.common.weight.DuplicateClickProxy
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.LoginPresenter
import com.help10000.rms.mvp.view.LoginView
import com.help10000.rms.ui.base.BaseFragment
import com.help10000.rms.ui.utils.EventBusHelper
import kotlinx.android.synthetic.main.fragment_login.btn_login
import kotlinx.android.synthetic.main.fragment_register.btn_submit_register
import kotlinx.android.synthetic.main.fragment_register.et_affpwd_register
import kotlinx.android.synthetic.main.fragment_register.et_nickname
import kotlinx.android.synthetic.main.fragment_register.et_phone_register
import kotlinx.android.synthetic.main.fragment_register.et_pwd_register
import kotlinx.android.synthetic.main.fragment_register.et_smscode_register
import kotlinx.android.synthetic.main.fragment_register.tv_gologin
import kotlinx.android.synthetic.main.fragment_register.tv_send
import kotlinx.android.synthetic.main.fragment_register.tv_time


class RegisterFragment : BaseFragment<LoginPresenter, LoginView>(), LoginView,
    View.OnClickListener {
    companion object {

        @JvmStatic
        fun newInstance() = RegisterFragment()
    }

    override fun getlayoutId(): Int {
        return R.layout.fragment_register
    }

    override fun createPresenter(): LoginPresenter {
        return LoginPresenter(this)
    }

    override fun initView(rootView: View?) {

    }

    override fun initListener() {
        tv_gologin.setOnClickListener(DuplicateClickProxy(this))
        tv_send.setOnClickListener(DuplicateClickProxy(this));
        btn_submit_register.setOnClickListener(DuplicateClickProxy(this));
    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    var smsSend = false
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_gologin -> {
                EventBusHelper.post(LoginPageEvent(2))
            }
            R.id.tv_send -> {
                if (!ButtonUtils.isFastDoubleClick(R.id.tv_send) && tv_time.visibility == View.GONE) {
                    mPresenter.sendSms(et_phone_register.text.toString())
                }
            }
            R.id.btn_submit_register -> {
//                EventBusHelper.post(
//                    LoginPageEvent(
//                        "943719",
//                        "123456"
//                    )
//                )
//                return
                if (!smsSend) {
                    ToastUtil.show("请先发送验证码")
                    return
                }
                mPresenter.register(
                    et_phone_register.text.toString(),
                    et_smscode_register.text.toString(),
                    et_nickname.text.toString(),
                    et_pwd_register.text.toString(),
                    et_affpwd_register.text.toString()
                )
            }
        }
    }

    /**
     * 登陆成功
     */
    override fun loginSuccess() {

    }
    override fun onShow(){
        setLoginButtonEnabled(true)
    }
    private fun setLoginButtonEnabled(enabled: Boolean) {
        btn_login.isEnabled = enabled // 控制点击响应
        // 根据状态设置背景
        val backgroundRes = if (enabled) {
            R.drawable.shape_login_button // 正常可点击背景
        } else {
            R.drawable.shape_login_button_pressed // 禁止点击背景（灰色/按压态）
        }
        btn_login.setBackgroundResource(backgroundRes)
    }
    override fun onDestroy() {
        super.onDestroy()
        mCountDownTimer?.cancel()
    }

    override fun registerSucc() {
        ToastUtil.show("注册成功")
        EventBusHelper.post(
            LoginPageEvent(
                et_phone_register.text.toString(),
                et_pwd_register.text.toString()
            )
        )
    }

    var mCountDownTimer: CountDownTimer? = null
    override fun sendSmsSuccess() {
        smsSend = true
        tv_time.visibility = View.VISIBLE
        mCountDownTimer = object : CountDownTimer(30 * 1000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                tv_time.text = "剩余${millisUntilFinished / 1000}s"
            }

            override fun onFinish() {
                tv_time.visibility = View.GONE
                tv_time.text = "剩余0s"
                tv_send.text = "重新发送"
            }
        }.start()
    }

    override fun checkSMSSuccess() {

    }
}